<?php
//include XML Header (as response will be in xml format)
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
         header("Content-type: application/xhtml+xml"); } else {
         header("Content-type: text/xml");
}



session_start();
echo '<?xml version="1.0" encoding="utf-8"?>';
require "[system_path]system/framework.php";   // muss nacherher aufgerufen werden, da sonst Probleme mit Leerzeile

         
    $text = $_SERVER['REQUEST_URI'];
    file::createTextFile("/tmp/result.txt", $text, true);
         
         
         
    //$table = $_REQUEST['table'];    
    $table = "content";
    
    
    //$table = sqlGetDomainTable($table);

//deletes single node in db
function deleteSingleNode($id){
        global $table;
        $sql = "DELETE FROM ".$table." WHERE ID = ".$id;  
        //alert(sql);
        db::sqlExec($sql);
    }
    
//deletes all child nodes of the item
function deleteBranch($pid){
//  and GUID='".$_SESSION["id"]."'"
    global $table;
    $s_sql = "SELECT ID, item_order FROM ".$table." WHERE item_parent_id=".$pid;
    $res = mysql_query($s_sql);
    if($res){
        while($row=mysql_fetch_array($res)){
            deleteBranch($row['ID']);
            deleteSingleNode($row['ID']);
        }
    }
}

//XML HEADER

//include XML Header (as response will be in xml format)
/*if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
         header("Content-type: application/xhtml+xml"); } else {
         header("Content-type: text/xml");
}
echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");*/ 

if(isset($_GET["!nativeeditor_status"]) && $_GET["!nativeeditor_status"]=="inserted"){

    //INSERT
    
    //insert new row
    
        
    
    //, Url_Rewrite, Template, filename_text
    $sql =     "INSERT INTO ".$table."(Titel,item_parent_id,item_order) ";
    $sql.=     "VALUES ('".addslashes(utf8_decode($_GET["tr_text"]))."',".$_GET["tr_pid"].",".$_GET["tr_order"].")";
    
        $res = mysql_query($sql);
        $newId = mysql_insert_id();
        
        
        
    //update items orders on the level where node was added
    $sql_uorders = "UPDATE ".$table." SET item_order=item_order+1 WHERE item_parent_id='".$_GET["tr_pid"]."' AND item_order>=".$_GET["tr_order"]." and id!='".$newId."'";   //." and GUID='".$_SESSION["id"]."'";
        $res = mysql_query($sql_uorders);
        
    //set value to use in response
    $action = "insert";
    
    
}else if(isset($_GET["!nativeeditor_status"]) && $_GET["!nativeeditor_status"]=="deleted"){

    //DELETE
    
    

    
    //updateitems order on the level where node was deleted
    $sql_uorders = "UPDATE ".$table." SET item_order=item_order-1 WHERE item_parent_id=".$_GET["tr_pid"]." AND item_order>".($_GET["tr_order"]);
    //." and GUID='".$_SESSION["id"]."'";
    
    
    
    
    
        //delete all nested nodes and current node
        deleteBranch($_GET["tr_id"]);
        deleteSingleNode($_GET["tr_id"]);
        $res = mysql_query($sql_uorders);
    //set values to use in response
    $newId = $_GET["tr_id"];
    $action = "delete";
    
}else{

    //UPDATE and Drag-n-Drop
    
    //get information about node parent and order before update
    $sql_getoldparent = "SELECT item_parent_id,item_order FROM ".$table." WHERE ID =".$_GET["tr_id"];
    // and GUID='".$_SESSION["id"]."'";
    //$res = mysql_query($sql_getoldparent);
    $old_values = db::getDataLine($table, "id ='".$_GET["tr_id"]."'");
    
    
    
    
   /* if (!$res)
        {
        echo 'Query failed: ' . mysql_error()." <br>SQL:".$sql_getoldparent."<br><br>";
        }   */
    
    
    
   // $old_values = mysql_fetch_array($res);
    //update node info 
//,item_desc = '".addslashes($_GET["ud_description"])."'
    $sql =     "UPDATE ".$table." SET title = '".addslashes(utf8_decode($_GET["tr_text"]))."',item_parent_id = '".$_GET["tr_pid"]."',item_order = ".$_GET["tr_order"]." WHERE id='".$_GET["tr_id"]."'";   //." and GUID='".$_SESSION["id"]."'";
    
    /*if ($table == "snapol_menu") {  
        $sql =     "UPDATE ".$table." SET item_parent_id = ".$_GET["tr_pid"].",item_order = ".$_GET["tr_order"]." WHERE id=".$_GET["tr_id"];   //." and GUID='".$_SESSION["id"]."'";
    }*/
    
    
    //update nodes order on old node level (after drag-n-drop node level can be changed)
    $sql_uorders_old = "UPDATE ".$table." SET item_order=item_order-1 WHERE item_parent_id='".$old_values['id']."' AND item_order>".$old_values['item_order']." AND id<>'".$_GET["tr_id"]."'";
    //." and GUID='".$_SESSION["id"]."'";
    //update nodes order on current node level
    $sql_uorders_new = "UPDATE ".$table." SET item_order=item_order+1 WHERE item_parent_id='".$_GET["tr_pid"]."' AND item_order>=".$_GET["tr_order"]." and id<>'".$_GET["tr_id"]."'";
    //." and GUID='".$_SESSION["id"]."'";
      /*  $res = mysql_query($sql);
        $res = mysql_query($sql_uorders_old);
        $res = mysql_query($sql_uorders_new); */
    
        db::sqlExec($sql);
        db::sqlExec($sql_uorders_old);
        db::sqlExec($sql_uorders_new);
    
    
    
    //set values to include in response
    $newId = $_GET["tr_id"];
    $action = "update";
}
?>
<data>
    <action type="<?php echo $action; ?>" sid="<?php echo $_GET["tr_id"]; ?>" tid="<?php echo $newId; ?>" />
</data>
